package com.wuliu.servlet;


import com.wuliu.dao.*;
import com.wuliu.dao.impl.*;
import com.wuliu.model.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/page1")
public class FirstServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        FirstImageDAO firstImageDAO=new FirstImageDAOImpl();
        List<FirstImage> firstImages=firstImageDAO.findAll();
        req.setAttribute("firstImages",firstImages);
//        System.out.println(firstImages);

        NewsClassDAO newsClassDAO =new NewsClassDAOImpl();
        List<NewsClass>newsClasses =newsClassDAO.findAll();
        req.setAttribute("newsClasses",newsClasses);

        NewsDAO newsDAO =new NewsDAOImpl();
        List<News> newsList1=newsDAO.findByCid(newsClasses.get(0).getId());
        req.setAttribute("newsList1",newsList1);
        List<News> newsList2=newsDAO.findByCid(newsClasses.get(1).getId());
        req.setAttribute("newsList2",newsList2);
        List<News> newsList3=newsDAO.findByCid(newsClasses.get(1).getId());
        req.setAttribute("newsList3",newsList3);

        BusinessClassDAO businessClassDAO=new BusinessClassDAOImpl();
        List<BusinessClass> businessClasses=businessClassDAO.findAll();
        req.setAttribute("businessClasses",businessClasses);

        BusinessDAO businessDAO=new BusinessDAOImpl();
        List<Business> businesses=businessDAO.findAll();
        req.setAttribute("businesses",businesses);
        req.getRequestDispatcher("/WEB-INF/view/first.jsp").forward(req,resp);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String type= req.getParameter("type");
        System.out.println(type);
        if (type.equals("login")){
            String account = req.getParameter("account");
            String password = req.getParameter("password");
            boolean admin =Boolean.valueOf(req.getParameter("admin"));
            System.out.println(account + password);
            UserDAO userDAO = new UserDAOImpl();
            User user = userDAO.findByAccount(account);
            if (user == null) {
                req.setAttribute("msg", "账号不存在");
                req.getRequestDispatcher("/WEB-INF/view/first.jsp").forward(req, resp);
                return;
            } else if (!user.getPassword().equals(password)) {
                req.setAttribute("msg", "密码错误");
                req.getRequestDispatcher("/WEB-INF/view/first.jsp").forward(req, resp);
                return;
            }
            req.getSession().setAttribute("user", user);
            req.getSession().setAttribute("admin",admin);
            resp.sendRedirect("/page1");
        }else if (type.equals("register")){
            String account = req.getParameter("account");
            String password = req.getParameter("password");
            String name = req.getParameter("name");
            String gender = req.getParameter("gender");
            boolean admin =Boolean.valueOf(req.getParameter("admin"));
            User user = new User(null, account, password, name, gender,admin);
            System.out.println(user);
//        System.out.println(user);
            UserDAO userDAO = new UserDAOImpl();
            User user1 = userDAO.findByAccount(account);
            if (user1 != null) {
                req.setAttribute("msg", "该账号已经存在");
                req.getRequestDispatcher("/WEB-INF/view/register.jsp").forward(req, resp);
                return;
            }

            userDAO.add(user);
            req.setAttribute("msg", "注册成功");
            resp.sendRedirect("/login");

        }

    }
}
